/**
 * 
 */
package si.unimb.src.wifisms;

import java.util.ArrayList;

import si.unimb.src.wifisms.ui.CheckableItem;
import si.unimb.src.wifisms.ui.CheckboxIconArrayAdapter;
import si.unimb.src.wifisms.util.SettingsType;
import si.unimb.src.wifisms.util.Utility;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Nastavitveni razred.
 * @author perox
 */
public class Settings extends Activity implements OnItemClickListener, OnClickListener {
	
	private ListView MenuList;
	private CheckboxIconArrayAdapter ciaa;
	private Button saveSettings;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings); 
        
        saveSettings = (Button)findViewById(R.id.ShraniNastavitveButton);
        
        ArrayList<CheckableItem> arci = new  ArrayList<CheckableItem>();
        arci.add(new CheckableItem("Dovoli preko WIFI", si.unimb.src.wifisms.entity.Settings.getAllowWifi(this), SettingsType.WIFI));
        arci.add(new CheckableItem("Dovoli preko 3G/EDGE/GPRS", si.unimb.src.wifisms.entity.Settings.getAllowMobile(this), SettingsType.EDGE));
        
        MenuList = (ListView)findViewById(R.id.MenuList);
        ciaa = new CheckboxIconArrayAdapter(this, R.layout.image_list_view, arci);
        
	    MenuList.setAdapter(ciaa);
	    MenuList.setOnItemClickListener(this);
	    
	    saveSettings.setOnClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		CheckableItem item = (CheckableItem)MenuList.getItemAtPosition(pos);
        item.toggle();
        
        // disclaimer pri mobile
		if(pos == 1 && item.isChecked()) {
		
			Utility.showAlert(this, "Opozorilo", "Raba mobilnega omrežja za prenos podatkov vam lahko povzroči dodatne stroške.");
			
		}
        
        ciaa.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		
		if(v.equals(saveSettings)) {
			
			CheckableItem wifi = (CheckableItem)MenuList.getItemAtPosition(0);
			CheckableItem mobile = (CheckableItem)MenuList.getItemAtPosition(1);
			
			if(!wifi.isChecked() && !mobile.isChecked()) {
			
				Utility.makeToastNotification(this, "Ni bilo shranjeno, saj takšne nastavitve nimajo smisla!");
				
			} else {
				
				si.unimb.src.wifisms.entity.Settings.setAllowWifi(this, wifi.isChecked());
				si.unimb.src.wifisms.entity.Settings.setAllowMobile(this, mobile.isChecked());
				Utility.makeToastNotification(this, "Shranjeno.");
				
			}
			
		}
		
	}
}
